package com.jims.master.common.utils;

import com.jims.master.modules.sys.utils.netease.UUIDUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @author jict
 * @Title:
 * @Description: （文件上传）
 * @return
 * @date 2017/11/20
 * @throws
 */
public class UploadUtils {

    /**
     * @param file 文件数组
     * @param pathUrl
     * @return
     * @throws
     * @Title:
     * @Description: （文件上传）
     * @author jict
     * @date 2017/11/20
     */
    public static String savePics(MultipartFile file, String pathUrl, HttpServletRequest request) throws Exception {
        //获取项目路径
        String home = request.getSession().getServletContext().getRealPath("");
        //避免文件名重复，采用uuid+原名称
        String str = UUIDUtil.getUUID();
        String headPath = str+file.getOriginalFilename();
        pathUrl = pathUrl + File.separator + headPath;
        File fileTemp = new File(home + pathUrl);
        if (!fileTemp.exists())
            fileTemp.mkdirs();
        //上传文件
        file.transferTo(fileTemp);
        return pathUrl;
    }
}
